package com.yxhl.zoume.core.officialcar.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.car.fragment.common.CarSelectPassengerFragment;
import com.yxhl.zoume.core.officialcar.fragment.CallCarForOthersFragment;
import com.yxhl.zoume.core.car.model.CarOtherPassengerEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.domain.interactor.ZMEventSubscriber;
import com.yxhl.zoume.utils.RegexUtils;
import com.yxhl.zoume.utils.ZMDialog;

import java.util.List;

import butterknife.BindView;

//import com.yxhl.zoume.core.car.fragment.common.CallCarForOthersFragment;

public class CallCarForOthersActivity extends BaseActivityWithToolBar {

    public static final String FRAGMENT_CAR_SELECT_PASSENGER = "CarSelectPassengerFragment";
    public static final String FRAGMENT_CALL_CAR_FOR_OTHERS = "CallCarForOthersFragment";
    private static final String TAG = "CallCarForOthersAct";

    //widgets
    @BindView(R.id.frame_activity_call_car_for_others_content)
    FrameLayout mContentFrame;


    //arguments

    private String mCurrentShowFragmentTab;
    private String mSelectPassengerMobile;

    private boolean mNeedSendMsg = false;

    public static Intent getCallingIntent(Activity activity) {
        Intent intent = new Intent(activity, CallCarForOthersActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeShow();
    }


    private void initializeShow() {
        displayOneFragment(FRAGMENT_CAR_SELECT_PASSENGER);
        displayOneFragment(FRAGMENT_CALL_CAR_FOR_OTHERS);
    }

    /**
     * 显示特定Tag的Fragment,如果是第一次,则新建并添加
     *
     * @param fragmentTab
     */
    public void displayOneFragment(String fragmentTab) {
        FragmentManager manager = getSupportFragmentManager();
        if (manager != null) {
            FragmentTransaction transaction = manager.beginTransaction();

            List<Fragment> fragments = manager.getFragments();
            if (fragments != null && fragments.size() > 0) {
                for (Fragment f : fragments) {
                    if (f != null) {
                        transaction.hide(f);
                    }
                }
            }

            Fragment fragmentByTag = manager.findFragmentByTag(fragmentTab);
            if (fragmentByTag != null) {
                transaction.show(fragmentByTag);
            } else {
                transaction.add(R.id.frame_activity_call_car_for_others_content, fragmentProvider(fragmentTab), fragmentTab);
            }

            transaction.commitAllowingStateLoss();
            mCurrentShowFragmentTab = fragmentTab;
        }
    }

    private Fragment fragmentProvider(String fragmentTag) {
        switch (fragmentTag) {
            case FRAGMENT_CALL_CAR_FOR_OTHERS:
                return CallCarForOthersFragment.newInstance();
            case FRAGMENT_CAR_SELECT_PASSENGER:
                return CarSelectPassengerFragment.newInstance();
        }
        return CallCarForOthersFragment.newInstance();
    }

    @Override
    public void onBackPressed() {
        switch (mCurrentShowFragmentTab) {
            case FRAGMENT_CAR_SELECT_PASSENGER:
                displayOneFragment(FRAGMENT_CALL_CAR_FOR_OTHERS);
                break;
            case FRAGMENT_CALL_CAR_FOR_OTHERS:
                super.onBackPressed();
                break;
        }
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

        if (!TextUtils.isEmpty(mSelectPassengerMobile)) {
            if (RegexUtils.isValidMobile(mSelectPassengerMobile)) {
                RxBus.getInstance().send(new CarOtherPassengerEvent(mSelectPassengerMobile, mNeedSendMsg));
                finish();
                return;
            }
        }

        ZMDialog dialog = new ZMDialog.Builder(this)
                .setMessage(getResources().getString(R.string.dialog_car_validate_mobile_number_content))
                .setPositiveMessage(getResources().getString(R.string.dialog_knew_it))
                .setPositiveClickEvent(new ZMEventSubscriber())
                .show();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_call_car_for_others;
    }

    @Override
    protected void initializeInjector() {

    }

    public void setSelectPassengerMobile(String selectPassengerMobile) {
        mSelectPassengerMobile = selectPassengerMobile;
    }

    public void setNeedSendMsg(boolean needSendMsg) {
        mNeedSendMsg = needSendMsg;
    }
}
